
package com.wugh.fast.security.social.app.wechat.config;

import com.wugh.fast.kernel.core.props.FastProperties;
import com.wugh.fast.kernel.core.props.SecurityProperties;
import com.wugh.fast.kernel.core.props.SocialApplicationProperties;
import com.wugh.fast.security.social.app.wechat.connect.WechatConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactory;

/**
 * 微信登录配置
 *
 * @author zhailiang
 */
@Configuration
@ConditionalOnProperty(prefix = SecurityProperties.PROPERTY_PREFIX + ".wechat", name = "app-id")
public class WechatSocialConfigurerAdapter extends SocialConfigurerAdapter {

    @Autowired
    private FastProperties fastProperties;

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment environment) {
        connectionFactoryConfigurer.addConnectionFactory(createConnectionFactory());
    }

    protected ConnectionFactory<?> createConnectionFactory() {
        SocialApplicationProperties wechat = fastProperties.getSecurity().getSocial().getWechat();
        return new WechatConnectionFactory(wechat.getProviderId(), wechat.getAppId(), wechat.getAppSecret());
    }

}
